import { _decorator, Component, Node, Prefab, math, instantiate, Vec2, Vec3, macro, buildShadowPass, Label, BoxCollider, Pool } from 'cc';
import { Bullet } from '../bullet/bullet';
import { BulletProp } from '../bullet/BulletProp';
import { constant } from '../constant';
import { enemyplane } from '../enemyplane/enemyplane';
import { planeController } from '../planeController';
import { AudioManager } from './AudioManager';
import { PoolManager } from './PoolManager';
import { boss } from '../enemyplane/boss';
const { ccclass, property } = _decorator;

@ccclass('gameManeger')
export class gameManeger extends Component {
    //bullet
    @property(Prefab)
    public bullet02: Prefab = null;
    @property(Prefab)
    public missile: Prefab = null;
    //propType
    @property(Prefab)
    public propH: Prefab = null;
    @property(Prefab)
    public propM: Prefab = null;
    @property(Prefab)
    public propS: Prefab = null;
    @property
    public propSpeedX = 0.3;
    @property
    public propSpeedZ = 0.3;
    //enemy
    @property(Prefab)
    public enemy01: Prefab = null;
    @property(Prefab)
    public enemy02: Prefab = null;
    @property(Prefab)
    public boss: Prefab = null;
    @property(Node)
    public bulletRoot: Node = null;
    @property(Prefab)
    public smallExplode: Prefab = null
    @property
    public createEnemyTime = 1;
    @property
    public createBossTime = 10;
    @property
    public enemy1Speed = 0.5;
    @property
    public enemy2Speed = 0.7;
    //流程控制
    @property(planeController)
    public planeController: planeController = null;
    @property(Node)
    public gamePage: Node;
    @property(Node)
    public gameOverPage: Node;
    @property(Label)
    scoreLabel: Label = null;
    //audio
    public audioEffect: AudioManager = null;

    //敌机出现周期
    private _curCreateEnemyTime = 0;
    private curCreateBossTime = 0;
    private _combinationInterval = 1;//组合方式变换间隔时间
    public isGameStart = false;
    private _score = 0;
    start() {

    }

    update(deltaTime: number) {
        if (!this.isGameStart)
            return;
        if (this.planeController._isDead) {
            this.gameOver();
            return;
        }
        //周期性产生敌机
        this._curCreateEnemyTime += deltaTime;
        this.curCreateBossTime += deltaTime;
        //console.log(this.curCreateBossTime);
        //生成Boss
        if (this.curCreateBossTime > this.createBossTime) {
            this.curCreateBossTime = 0;
            this.createBoss();
            console.log("successBoss");
        }
        //单架敌机生成 一阶段
        if (this._combinationInterval == constant.Combination.PLAN1) {
            if (this._curCreateEnemyTime > this.createEnemyTime) {
                this._curCreateEnemyTime = 0;
                this.createEnemy();
            }
        }//二阶段 一字 单架 组合出现
        else if (this._combinationInterval == constant.Combination.PLAN2) {
            if (this._curCreateEnemyTime > this.createEnemyTime * 0.9) {//周期变为原来的0.9
                const randomCombination = math.randomRangeInt(1, 3);
                if (randomCombination == constant.Combination.PLAN2) {
                    this.createCombination1();
                }
                else {
                    this.createEnemy();
                }
                this._curCreateEnemyTime = 0;
            }
        }//三阶段 一字 单架 v字组合出现
        else {
            if (this._curCreateEnemyTime > this.createEnemyTime * 0.8) {//周期变为原来的0.8
                const randomCombination = math.randomRangeInt(1, 4);
                if (randomCombination == constant.Combination.PLAN2) {
                    this.createCombination1();
                }
                else if (randomCombination == constant.Combination.PLAN3) {
                    this.createCombinationV();
                }
                else {
                    this.createEnemy();
                }
                this._curCreateEnemyTime = 0;
            }

        }
    }
    //初始化
    public _init() {
        //this.changePlaneMode();
        this._curCreateEnemyTime = 0;
        this._combinationInterval = constant.Combination.PLAN1;
        this.planeController.node.setPosition(0, 0, 15);
    }
    //改变敌机出现类型
    private changePlaneMode() {
        this.schedule(this.modeChanged, 10, macro.REPEAT_FOREVER);//计时器
    }
    //callback
    private modeChanged() {
        this._combinationInterval++;//进入下一阶段 难度增加 敌机出现方式改变
        this.createBulletProp();
    }
    //实例化Boss
    private createBoss() {
        const _boss = PoolManager.instance().getNode(this.boss, this.node);
        const bossComp = _boss.getComponent(boss);
        bossComp.show(this, 0.2);
        _boss.setPosition(0, 0, -40);
    }
    //实例化敌机
    private createEnemy() {
        const type = math.randomRangeInt(1, 3);//随机的敌机类型
        let enemyPlane: Prefab;
        let enemySpeed = 0;
        if (type == 1) {
            enemyPlane = this.enemy01;
            enemySpeed = this.enemy1Speed;
        }
        else {
            enemyPlane = this.enemy02;
            enemySpeed = this.enemy2Speed;
        }
        // const enemy = instantiate(enemyPlane);
        // enemy.setParent(this.node);
        const enemy = PoolManager.instance().getNode(enemyPlane, this.node);
        const enemyComp = enemy.getComponent(enemyplane);
        enemyComp.show(this, enemySpeed, true);
        const randomPos = math.randomRangeInt(-20, 21);
        enemy.setPosition(randomPos, 0, - 50);
    }
    //创建一字型敌机组合
    private createCombination1() {
        const enemyArray = new Array<Node>(5);
        for (let i = 0; i < enemyArray.length; i++) {
            // enemyArray[i] = instantiate(this.enemy01);//实例化敌机类型
            enemyArray[i] = PoolManager.instance().getNode(this.enemy01, this.node);
            const element = enemyArray[i];
            // element.parent = this.node;
            element.setPosition(-20 + i * 10, 0, -50);//飞机间隔为10
            const enemyComp = element.getComponent(enemyplane);
            enemyComp.show(this, this.enemy1Speed, false);
        }
    }
    //创建V字型敌机组合
    private createCombinationV() {
        const enemyArray = new Array<Node>(5);
        var distanceArray: number[] = [-56, -53, -50, -53, -56];//z方向上的间隔
        for (let i = 0; i < enemyArray.length; i++) {
            //enemyArray[i] = instantiate(this.enemy02);
            enemyArray[i] = PoolManager.instance().getNode(this.enemy02, this.node);
            const element = enemyArray[i];
            //element.parent = this.node;
            element.setPosition(-20 + i * 10, 0, distanceArray[i]);//飞机间隔为10
            const enemyComp = element.getComponent(enemyplane);
            enemyComp.show(this, this.enemy2Speed, false);
        }
    }
    //创建boss导弹
    public createBossMissile(targetPos: Vec3) {
        const bullet = PoolManager.instance().getNode(this.missile, this.bulletRoot);
        bullet.setPosition(targetPos.x, targetPos.y, targetPos.z + 5);//setpositon相对父节点的位置,默认应该时父节点位置
        const bulletComp = bullet.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletComp.show(0.8, true, constant.Direction.MID);//传递实例化子弹所需要的参数
        const colliderComp = bullet.getComponent(BoxCollider);
        colliderComp.setGroup(constant.CollisionType.BOSSMISSILE);
        colliderComp.setMask(constant.CollisionType.PLANE);
    }
    //创建敌机子弹
    public createEnemyBullet(targetPos: Vec3) {
        // const bullet = instantiate(this.bullet02);//实例化子弹
        // bullet.setParent(this.bulletRoot);//设置子弹父节点
        const bullet = PoolManager.instance().getNode(this.bullet02, this.bulletRoot);
        bullet.setPosition(targetPos.x, targetPos.y, targetPos.z + 5);//setpositon相对父节点的位置,默认应该时父节点位置
        const bulletComp = bullet.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletComp.show(0.8, true, constant.Direction.MID);//传递实例化子弹所需要的参数
        const colliderComp = bullet.getComponent(BoxCollider);
        colliderComp.setGroup(constant.CollisionType.ENEMY_BULLET);
        colliderComp.setMask(constant.CollisionType.PLANE);
    }
    //敌机死亡动画
    public createEffect(pos: Vec3) {
        const effect = PoolManager.instance().getNode(this.smallExplode, this.node);
        effect.setPosition(pos);
    }
    //增加分数
    public addScore() {
        this._score++;
        this.scoreLabel.string = this._score.toString();

    }
    //生成道具
    private createBulletProp() {
        const rangdom = math.randomRangeInt(1, 4);
        let prefab: Prefab = null;
        if (rangdom == constant.BulletType.BULLET_H) {
            prefab = this.propH;
        }
        else if (rangdom == constant.BulletType.BULLET_M) {
            prefab = this.propM;
        }
        else {
            prefab = this.propS;
        }
        // const prop = instantiate(prefab);
        // prop.setParent(this.node);
        const prop = PoolManager.instance().getNode(prefab, this.node);
        prop.setPosition(0, 0, -50);
        const propComp = prop.getComponent(BulletProp);
        propComp.show(this.propSpeedX, this.propSpeedX);
    }
    //播放音效
    public playAudioEffect(name: string) {
        this.audioEffect.play(name);
    }
    //初始化游戏
    public gameStart() {
        this.isGameStart = true;
        this.changePlaneMode();
        this._score = 0;
        this.scoreLabel.string = this._score.toString();
        this.planeController.init();
    }
    //重新开始游戏
    public gameReStart() {
        this.gameOverPage.active = false;
        this.gamePage.active = true;
        this.gameStart();
        this._curCreateEnemyTime = 0;
        this.curCreateBossTime = 0;
        this._combinationInterval = constant.Combination.PLAN1;
        this.planeController.node.setPosition(0, 0, 15);
    }
    //游戏结束
    private gameOver() {
        this.planeController._isShooting = false;
        this.gamePage.active = false;
        this.gameOverPage.active = true;
        this.isGameStart = false;
        this.unschedule(this.modeChanged);
        this._destroyAll();
    }
    //销毁节点
    private _destroyAll() {
        let children = this.node.children;
        let length = children.length;
        let i = 0;
        for (i = length - 1; i >= 0; i--) {
            const child = children[i];
            PoolManager.instance().putNode(child);
            // child.destroy();
        }

        children = this.bulletRoot.children;
        length = children.length;
        for (i = length - 1; i >= 0; i--) {
            const child = children[i];
            PoolManager.instance().putNode(child);
            // child.destroy();
        }
    }
    // public gameOver() {
    //     this.isGameStart = false;
    //     this.gamePage.active = false;
    //     this.gameOverPage.active = true;
    //     this.gameOverScore.string = this._score.toString();
    //     this.overAnim.play();
    //     this._isShooting = false;
    //     // this.playerPlane.init();
    //     this.unschedule(this._modeChanged);
    //     this._destroyAll();
    // }
}

